//
//  LearningHistoryService.m
//  GC_OC
//
//  Created by  Quan He on 2022/4/10.
//

#import "LearningHistoryService.h"

@interface LearningHistoryService()
@property (nonatomic, strong) NSMutableArray *historyList;
@end

@implementation LearningHistoryService


+ (instancetype)sharedInstance
{
    static LearningHistoryService *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [[LearningHistoryService alloc] init];
        }
    });
    return manager;
}

- (instancetype)init
{
    if (self = [super init]) {
        _historyList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (NSMutableArray *)getLearningHistoryInfo {
    NSMutableDictionary *dataDic = [self readData];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic allValues]];
    NSSortDescriptor *priceDescriptor = [NSSortDescriptor
                                        sortDescriptorWithKey:@"time"
                                        ascending:NO
                                        selector:@selector(compare:)];

    [array sortUsingDescriptors:@[priceDescriptor]];
    return array;
}

- (void)saveHistoryInfo:(NSDictionary *)dic {
    //获得系统时间
    NSDate *  senddate = [NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYYMMdd"];
    NSString * key = [dateformatter stringFromDate:senddate];

    NSMutableDictionary *dataDic = [self readData];
    NSMutableArray *contentArr = [NSMutableArray array];
    
    NSMutableDictionary *infoDic = [NSMutableDictionary dictionary];
    
    if ([dataDic.allKeys containsObject:key]) {
        infoDic = [NSMutableDictionary dictionaryWithDictionary:dataDic[key]];
        contentArr = [NSMutableArray arrayWithArray:infoDic[@"content"]];
    }
    for (int i=0; i<contentArr.count; i++) {
        NSDictionary *theDic = contentArr[i];
        NSString *idString = theDic[@"id"];
        if ([idString isEqualToString:dic[@"id"]] && [theDic[@"audio_num"] isEqualToString:dic[@"audio_num"]]) {
            [contentArr removeObjectAtIndex:i];
            i--;
        }
    }
    
    [contentArr insertObject:dic atIndex:0];
    
    infoDic[@"content"] = contentArr;
    infoDic[@"time"] = key;
    dataDic[key] = infoDic;
    
    [self saveValue:dataDic];
}

- (void)removeInfo:(NSDictionary *)dic {
    //获得系统时间
    NSDate *  senddate = [NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYYMMdd"];
    NSString * key = [dateformatter stringFromDate:senddate];

    NSMutableDictionary *dataDic = [self readData];
    NSMutableArray *contentArr = [NSMutableArray array];
    
    NSMutableDictionary *infoDic = [NSMutableDictionary dictionary];
    
    if ([dataDic.allKeys containsObject:key]) {
        infoDic = [NSMutableDictionary dictionaryWithDictionary:dataDic[key]];
        contentArr = [NSMutableArray arrayWithArray:infoDic[@"content"]];
    }
    for (int i=0; i<contentArr.count; i++) {
        NSDictionary *theDic = contentArr[i];
        NSString *idString = theDic[@"id"];
        if ([idString isEqualToString:dic[@"id"]] && [theDic[@"audio_num"] isEqualToString:dic[@"audio_num"]]) {
            [contentArr removeObjectAtIndex:i];
            i--;
        }
    }
        
    if (contentArr.count == 0) {
        [dataDic removeObjectForKey:key];
    }else {
        infoDic[@"content"] = contentArr;
        infoDic[@"time"] = key;
        dataDic[key] = infoDic;
    }
    
    [self saveValue:dataDic];
}

- (void)clearAllInfo {
    [self saveValue:[NSMutableDictionary dictionary]];
}

- (void)saveValue:(NSMutableDictionary *)dic {
    NSString *filePath = [self filePath];
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    [dic writeToURL:fileUrl atomically:YES];
}

- (NSMutableDictionary *)readData {
    NSString *filePath = [self filePath];
    NSMutableDictionary *products = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    if (products == nil) {
        products = [NSMutableDictionary dictionary];
    }
    return products;
}

- (NSString *)filePath {
    NSString *cachePatch = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [cachePatch stringByAppendingPathComponent:@"LearnHistory"];
    return filePath;
}

+ (NSString *)timeTrainsform:(NSString *)timeString {
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYYMMdd"];
    NSDate *date = [dateformatter dateFromString:timeString];
    [dateformatter setDateFormat:@"YYYY-M-dd"];
    NSString * time = [dateformatter stringFromDate:date];
    return time;
}
@end
